package com.flymobi.admin;

import java.io.File;
import java.util.List;

import com.flymobi.fw.modules.SM;
import com.flymobi.fw.util.FileUtil;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.Manager;
import com.flymobi.po.PageBlock;
import com.flymobi.po.PageBlockContent;
import com.flymobi.po.Pindao;
import com.flymobi.util.C;
import com.flymobi.util.Pagination;
import com.flymobi.util.WebContext;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Oct 21, 2008 11:16:42 AM
 * @version $Id: ManagerAction.java,v 1.1 2010/04/09 03:27:08 airsou Exp $
 */
public class PageBlockContentAction extends WebAction {
	/*
	 * private String method; private String id;
	 */
	private PageBlockContent pageBlockContent;

	private String pageblockid;
	
	private String pindaoid;

	/**
	 * @return the pindaoid
	 */
	public String getPindaoid() {
		return pindaoid;
	}

	/**
	 * @param pindaoid the pindaoid to set
	 */
	public void setPindaoid(String pindaoid) {
		this.pindaoid = pindaoid;
	}

	/**
	 * @return the pageBlockContent
	 */
	public PageBlockContent getPageBlockContent() {
		return pageBlockContent;
	}

	/**
	 * @param pageBlockContent
	 *            the pageBlockContent to set
	 */
	public void setPageBlockContent(PageBlockContent pageBlockContent) {
		this.pageBlockContent = pageBlockContent;
	}

	/**
	 * @return the pageblockid
	 */
	public String getPageblockid() {
		return pageblockid;
	}

	/**
	 * @param pageblockid
	 *            the pageblockid to set
	 */
	public void setPageblockid(String pageblockid) {
		this.pageblockid = pageblockid;
	}

	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String webProcess(WebContext context, Manager manager) {
		if ("add".equals(method)) {
			List<Pindao> pindaoList=SM.PindaoService.list();
			context.put("pindaoList", pindaoList);
			List<PageBlock> blockList=SM.PageBlockService.list(0, 1000);
			context.put("blockList", blockList);
			return "edit";
		} else if ("edit".equals(method)) {
			List<Pindao> pindaoList=SM.PindaoService.list();
			context.put("pindaoList", pindaoList);
			List<PageBlock> blockList=SM.PageBlockService.list(0, 1000);
			context.put("blockList", blockList);
			pageBlockContent=SM.PageBlockContentService.get(id);
			return "edit";
		} else if ("delete".equals(method)) {
			SM.PageBlockContentService.delete(id);
			context.put("rmsg", C.TEXT.DELETE_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("deleteselect".equals(method)) {
			for (String item : selItems) {
				SM.PageBlockContentService.delete(item);
			}
			context.put("rmsg", C.TEXT.DELETE_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("upidx".equals(method)) {
			PageBlockContent pbc=SM.PageBlockContentService.get(id);
			pbc.setIdx(pageBlockContent.getIdx());
			SM.PageBlockContentService.save(pbc);
			context.put("rmsg", C.TEXT.OPERA_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		} else if ("up".equals(method)) {
			PageBlockContent pbc=SM.PageBlockContentService.get(id);
			PageBlockContent top=SM.PageBlockContentService.getTop(new Long(pageblockid));
			pbc.setIdx(top.getIdx()-1);
			SM.PageBlockContentService.save(pbc);
			context.put("rmsg", C.TEXT.OPERA_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		}else if ("save".equals(method)) {
			for (int i = 0; i < upload.size(); i++) {
				File file = upload.get(i);
				String fileName=uploadFileName.get(i);
				String path=FileUtil.upfile(file,fileName);
				pageBlockContent.setPath(path);
			}
			if(!this.isEmptyId(pageBlockContent)){
				PageBlockContent pbc=SM.PageBlockContentService.get(pageBlockContent.getId()+"");
				pbc.setContId(pageBlockContent.getContId());
				pbc.setIdx(pageBlockContent.getIdx());
				pbc.setPageBlockId(pageBlockContent.getPageBlockId());
				pbc.setPindaoId(pageBlockContent.getPindaoId());
				pbc.setType(pageBlockContent.getType());
				pbc.setImgshow(pageBlockContent.isImgshow());
				pbc.setNewline(pageBlockContent.isNewline());
				pbc.setPushword(pageBlockContent.getPushword());
				pbc.setCss(pageBlockContent.getCss());
				pbc.setImgCtl(pageBlockContent.getImgCtl());
				if(!StringUtil.isEmpty(pageBlockContent.getPath())){
					pbc.setPath(pageBlockContent.getPath());
				}
				pageBlockContent=pbc;
				SM.PageBlockContentService.save(pageBlockContent);
			}else{
				SM.PageBlockContentService.save(pageBlockContent);
				pageBlockContent.setIdx(0L);
				SM.PageBlockContentService.save(pageBlockContent);
			}
			context.put("rmsg", C.TEXT.SAVE_SUCCESS);
			method = "list";
			return this.webProcess(context, manager);
		}else if ("list".equals(method)) {
			List<PageBlockContent> pageBlockContentList=null;
			Pagination p =null;
			if(StringUtil.isEmpty(pindaoid)||"-1".equals(pindaoid)||"0".equals(pindaoid)){
				pageBlockContentList = SM.PageBlockContentService.list(new Long(pageblockid),
						startRow, pagePerSize);
				p = new Pagination(SM.PageBlockContentService.getCount(
						new Long(pageblockid)).intValue(), startRow, pagePerSize);
				p.setPageUrl("pageblockcontent.html?method=list&pageblockid="
						+ pageblockid);
				context.put("pagenation", p);
			}else{
				pageBlockContentList = SM.PageBlockContentService.list(new Long(pageblockid),new Long(pindaoid),
						startRow, pagePerSize);
			}
			context.put("pageBlockContentList", pageBlockContentList);

			List<Pindao> pindaoList=SM.PindaoService.list();
			context.put("pindaoList", pindaoList);
			return "list";
		} else {
			method = "list";
			return this.webProcess(context, manager);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.camel.admin.WebAction#getSysLog()
	 */
	@Override
	public String getSysLog() {
		String opr = "";

		return opr;
	}
}
